#lang r5rs
(define (cube x) (* x x x))
(define (integrate f from to accuracy)
  (define h (/ (- to from) accuracy))
  (define (ky n) (*
                  (cond ((or (= n 0) (= n accuracy)) 1)
                        ((= (remainder n 2) 0)       2)
                        ((= (remainder n 2) 1)       4)) 
                              
                  (f (+ from (* n h)))))
  (define (integrate-iter f from to sum iter)
    (if (> iter accuracy)
        sum
        (integrate-iter f (+ from h) to (+ sum (ky iter)) (+ iter 1))))
  (* (integrate-iter f from to 0 0) (/ h 3)))
(display (integrate cube 0 1 1000))